package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;
import org.json.JSONException;
/**
 *
 * @Title: Bp_Get_WGToKen
 * @Description: 宝安排水 网格 获取ToKen
 * @author: chieh
 * @date 2020-07-31 12:30:00
 *
 */
public class Bp_Get_WGToKen implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(Bp_Get_WGToKen.class);

	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {

		log.debug("-------------------------start Bp_Get_WGToKen 开始-----------------");

		//获取配置信息
		String URL = getProperty.get("WGH.URL");
		String USER = getProperty.get("WGH.USER");
		String PASSWORD = getProperty.get("WGH.PASSWORD");
		//String URL = "http://10.99.89.3:50006/WebService.asmx";
		//String USER = "psgs";
		//String PASSWORD = "Gktjk_123456";		
		
		log.debug("URL:" + URL+"	appId:" + USER+"	appSecret:" + PASSWORD);	
		String charset = "utf-8";
		int timeout = 30000;
		String SOAPAction="http://tempuri.org/Login"
		
		long startTime = System.currentTimeMillis();

		def reJson = "";
		try {
			
			def xmlbody = """<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
							  <soap:Body>
							    <Login xmlns="http://tempuri.org/">
							      <sUser>"""+USER+"""</sUser>
							      <sPass>"""+PASSWORD+"""</sPass>
								</Login>
							  </soap:Body>
							</soap:Envelope>""";

			log.debug("调用接口发送报文信息："+xmlbody);

			def xml=Util.callWebService(URL,xmlbody,charset,SOAPAction,timeout);
			xml = Util.Transform(xml); //去命令空间
			xml = new XmlParser().parseText(xml); //xml解析
			log.debug("ToKen调用结果返回：" + xml);

			String tokenId = null;
			String LoginResult = null;

			try{
				LoginResult =xml.Body.LoginResponse.LoginResult.text();		
				tokenId =xml.Body.LoginResponse.sToken.text();
				}catch (Exception e){
					tokenId="";
					log.error("EM:",e);
				};
				log.debug("tokenId:	"+tokenId);
				log.debug("LoginResult:	"+LoginResult);
				
				if(tokenId!=null&&LoginResult=='0') {
					try {
						eliteBean.update("update SYSC_PARAMETER set PARAMETERVALUE = '"+tokenId+"',MODIFIEDDATE=GETDATE() where PARAMETER_ID = 'WGHTOK'", "dataSource");
					} catch (Exception e) {
						tokenId = null;
					}
				}
			
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"BP智慧化网格化获取ToKen接口调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":\""+tokenId+"\"}";

		} catch (JSONException e) {
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"BP智慧化网格化获取ToKen接口处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":{}}";
		}

		return reJson;
	}
}